package com.lengtouzai.databaseexp.view.personaljournal;


import com.lengtouzai.databaseexp.entity.Journal;
import com.lengtouzai.databaseexp.entity.User;
import com.lengtouzai.databaseexp.event.OnJournalDeleteEvent;
import com.lengtouzai.databaseexp.view.journalcomponent.JournalComponent;
import com.lengtouzai.databaseexp.view.main.MainView;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import io.jmix.core.DataManager;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.flowui.UiComponents;
import io.jmix.flowui.component.textarea.JmixTextArea;
import io.jmix.flowui.component.textfield.TypedTextField;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.view.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;

import java.time.LocalDateTime;
import java.util.List;

@Route(value = "personal-journal-view", layout = MainView.class)
@ViewController("exp_PersonalJournalView")
@ViewDescriptor("personal-journal-view.xml")
public class PersonalJournalView extends StandardView {
    @Autowired
    private CurrentAuthentication currentAuthentication;
    @Autowired
    private DataManager dataManager;
    @Autowired
    private UiComponents uiComponents;
    @ViewComponent
    private VerticalLayout journalList;
    @ViewComponent
    private TypedTextField<Object> title;
    @ViewComponent
    private JmixTextArea content;

    @Subscribe
    public void onReady(final ReadyEvent event) {
        initJournal();
    }

    private void initJournal() {
        User current = (User) currentAuthentication.getUser();
        List<Journal> journals = dataManager.load(Journal.class)
                .query("select e from exp_Journal e where e.belongs = :user order by e.createdAt desc")
                .parameter("user", current).list();

        journalList.removeAll();
        for (Journal journal : journals) {
            JournalComponent journalComponent = uiComponents.create(JournalComponent.class);
            journalComponent.setJournal(journal);
            journalList.add(journalComponent);
        }
    }

    @Subscribe(id = "create", subject = "clickListener")
    public void onCreateClick(final ClickEvent<JmixButton> event) {
        Journal journal = dataManager.create(Journal.class);
        journal.setBelongs((User) currentAuthentication.getUser());
        journal.setTitle(title.getValue());
        journal.setContent(content.getValue());
        journal.setCreatedAt(LocalDateTime.now());
        journal.setUpdatedAt(LocalDateTime.now());
        dataManager.save(journal);

        JournalComponent journalComponent = uiComponents.create(JournalComponent.class);
        journalComponent.setJournal(journal);
        journalList.addComponentAsFirst(journalComponent);

        title.clear();
        content.clear();
    }

    @EventListener
    private void onJournalDelete(OnJournalDeleteEvent event) {
        initJournal();
    }
}